/*
 * @Author: long-xinyu2 2192763423@qq.com
 * @Date: 2025-04-01 10:09:57
 * @LastEditors: long-xinyu2 2192763423@qq.com
 * @LastEditTime: 2025-04-21 11:35:28
 * @FilePath: \algorithm\algorithm-training\lqb\acwing4_1.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#include<functional>
#include <bitset>
using namespace std;
using ull = unsigned long long;
using i128 = __int128_t;
//inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}//代替取模+
#define endl '\n'
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
#define int long long

const int N = 1e5+10;

void solve()
{  
    int n,k;
    cin >> n >> k;
    vector<string> arr(n + 10);
    for(int i = 1; i <= n; i ++)
    {
        int x;
        cin >> x;
        string tt = to_string(x);
        arr[i] = tt;
    }
    int l = 0;
    int r = 1e18;
    auto check = [&](int mid) {
        int cnt = 0;
        int last = 0;
        int sum = 0;
        for(int i = 1; i <= n; i ++)
        {
            int cnt5 = 0;
            int cnt2 = 0;
            int mux = 0;
            for(int j = 0; j < arr[i].size(); j ++ )
            {
                if(arr[i][j] == '5')
                {
                    cnt5 ++;
                    cnt += cnt2  * cnt5;
                    mux = cnt2 * cnt5;
                }
                else if(arr[i][j] == '2')
                {
                    cnt2 ++;
                }
            }
            if(mux > mid)
            {
                return false;
            }
            cnt += last * cnt5;
            last += cnt2;
            if(cnt > mid)
            {
                sum ++;
                if(sum == k)
                {
                    return false;
                }
                cnt = mux;
                last = cnt2;
            }
        }
        return true;
    };
    while(l < r)
    {
        int mid = (l + r) >> 1;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l << endl;
}
signed main()
{
    int t = 1;
    //cin >> t;
    while(t --)
    {
        solve();
    }
}
